import time

from selenium.webdriver.common.by import By

from base.BackgroundBase.back_base import BasePage, BaseHandle
from utils import UtilsDriver, element_is_exist


class ProductPage(BasePage):
    def __init__(self):
        super().__init__()
        self.frame = By.ID, "workspace"
        # self.input_box = By.CSS_SELECTOR, ".sDiv2"
        self.search = By.NAME, 'key_word'
        self.search_btn = By.CSS_SELECTOR, '.btn'
        self.msg = By.XPATH, "//tbody/tr/td[4]/*[text()='haier海尔 BC-93TMPF 93升单门冰箱']"

    # def find_input_box(self):
    #     return self.get_element(self.input_box)
    def find_frame(self):
        UtilsDriver.get_background_driver().switch_to.frame(self.get_element(self.frame))

    def find_search(self):
        return self.get_element(self.search)

    def find_search_btn(self):
        return self.get_element(self.search_btn)

    def find_msg(self):
        return self.get_element(self.msg)


class ProductHandle(BaseHandle):
    def __init__(self):
        self.product_page = ProductPage()

    def get_frame(self):
        # self.product_page.find_input_box().click()
        self.product_page.find_frame()

    def input_search_content(self, content):
        time.sleep(3)
        self.input_text(self.product_page.find_search(), content)

    def click_search_btn(self):
        time.sleep(3)
        self.product_page.find_search_btn().click()

    def get_msg(self):
        return self.product_page.find_msg().text


class ProductProxy:
    def __init__(self):
        self.product_handle = ProductHandle()
        self.driver = UtilsDriver.get_background_driver()

    def go_msg_page(self, content):
        self.product_handle.get_frame()
        self.product_handle.input_search_content(content)
        self.product_handle.click_search_btn()
        time.sleep(3)
        return element_is_exist(self.driver, self.product_handle.get_msg())
